package com.xpj.callable;

import java.util.concurrent.Callable;

public class MyFutureTask2<V> implements Runnable{

    private Callable<V> callable;
    private V v;
    private Object monitor = new Object();

    public MyFutureTask2(Callable<V> callable){
        this.callable = callable;
    }

    @Override
    public void run() {

        try {
            this.v = callable.call();
        } catch (Exception e) {
            e.printStackTrace();
        }

        synchronized (monitor){
            monitor.notify();
        }
    }

    public V get(){
        synchronized (monitor) {
            try {
                monitor.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return v;
    }
}
